﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using TTData;

namespace Thurn_und_Taxis.components
{
    /// <summary>
    /// Interaction logic for PlayerBox.xaml
    /// </summary>
    public partial class PlayerBox : UserControl
    {
        private String _nickname;
        private const Int32 DEFAULT_HEIGHT_CORNER = 20;
        private const Int32 DEFAULT_HEIGHT_WRAP = 18;
        private const Int32 DEFAULT_HEIGHT_CARRIGE = 122;
        private const Int32 DEFAULT_HEIGHT_BONUS = 30;
        private const Int32 DEFAULT_HEIGHT = 1024;
        private const Int32 DEFAULT_HEIGHT_CARD = 26;


        public PlayerBox()
        {
            _nickname = "[undefined]";
            InitializeComponent();
            ResizeVisualComponents();
        }

        public void UpdateNickname(String nickname) {
            _nickname = nickname;
            if (labelNickname != null) 
                labelNickname.Content = nickname;
        }

        public void UpdateHouse(String source)
        {
            try
            {
                Stream imageStreamSource = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read);
                PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageHouse != null)
                    imageHouse.Source = bitmapSource;
            }
            catch (Exception err)
            {

            }
        }

        public void UpdateHouseCount(Int32 value)
        {
            labelHousesCount.Content = Convert.ToInt32(labelHousesCount.Content) -  value;
        }

        public void UpdateImage(String source)
        {
            try
            {
                Stream imageStreamSource = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read);
                GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                if (imageCarrige != null)
                    imageCarrige.Source = bitmapSource;
            }
            catch (Exception err)
            {
                
            }
        }

        public void UpdateBonuses(BonusCard bonus)
        {
            try
            {
                Stream imageStreamSource = new FileStream(bonus.Source, FileMode.Open, FileAccess.Read, FileShare.Read);
                GifBitmapDecoder decoder = new GifBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
                BitmapSource bitmapSource = decoder.Frames[0];

                Image img = new Image();
                img.Height = DEFAULT_HEIGHT_BONUS * System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;
                img.Width = DEFAULT_HEIGHT_BONUS * System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;
                img.Source = bitmapSource;
                wrapBonuses.Children.Add(img);
            }
            catch (Exception err)
            {

            }
        }

        public void ChangeReadyState(Boolean isReady)
        {
            if (isReady)            
                labelNickname.Content = "*" + _nickname;                
            else
                labelNickname.Content = _nickname;                
        }

        public void Update2UserTurn(Boolean isTurn)
        {
            if (isTurn)
                labelNickname.Content = "***" + _nickname;
            else
                labelNickname.Content = _nickname; 
        }

        public void InsertPublicCard(String cardName, Int32 position)
        {
            String sourceFile = CityCard.GetCityCardNameImageSource(cardName);
            Stream imageStreamSource = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);
            PngBitmapDecoder decoder = new PngBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            try
            {
                Image img = new Image();
                img.Source = bitmapSource;
                img.Height = DEFAULT_HEIGHT_CARD * System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;

                if (position == 0)
                    wrapPublicCards.Children.Insert(0, img);
                else
                    wrapPublicCards.Children.Add(img);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public void ClearPublicCards()
        {
            wrapPublicCards.Children.Clear();
        }

        private void ResizeVisualComponents()
        {
            Double ratioHeight = System.Windows.SystemParameters.PrimaryScreenHeight / DEFAULT_HEIGHT;
            
            image_LeftDown.Height = DEFAULT_HEIGHT_CORNER * ratioHeight;
            image_RightDown.Height = DEFAULT_HEIGHT_CORNER * ratioHeight;
            image_RightUp.Height = DEFAULT_HEIGHT_CORNER * ratioHeight;
            image_LeftUp.Height = DEFAULT_HEIGHT_CORNER * ratioHeight;

            wrapMaskUp.Height = DEFAULT_HEIGHT_WRAP * ratioHeight;
            wrapMaskDown.Height = (DEFAULT_HEIGHT_WRAP-1) * ratioHeight;

            imageCarrige.Height = DEFAULT_HEIGHT_CARRIGE * ratioHeight;

        }
    }
}
